<template>
  <div class="index-page">
    <Back-top></Back-top>
    <Nav active-name="home"/>
    <div class="sec-01 flex-row-v-center">
      <div class="article-holder flex-1">
        <div class="carousel-box">
          <Carousel
            v-if="carouselList.length > 0"
            :autoplay="setting.autoplay"
            :autoplay-speed="setting.autoplaySpeed"
            :dots="setting.dots"
            :arrow="setting.arrow">
            <Carousel-item v-for="item in carouselList" :key="item.id">
              <div class="carousel-item"
                   :style="{background: 'url(' + item.img + ') no-repeat center center', backgroundSize: 'contain'}"
                   @click="goToArticlePage(item)"></div>
            </Carousel-item>
          </Carousel>
        </div>
        <div class="ad-box flex-row-v-center">
          <img src="~assets/img/icon-ad.png" alt=""/>
          <div class="content flex-1">
            <scroll-ad v-if="adList.length > 0" hover-color="#F58B01" inhover-color="#3A3D41"
                       :dataList="adList"></scroll-ad>
          </div>
        </div>
        <div class="hot-game-box">
          <div class="title">热门游戏</div>
          <div class="content flex-row-v-center">
            <div class="item" v-for="item in gameList" :key="item.id" @click="goToNewPage(item.url)">
              <img :src="item.img">
              <div class="name">{{ item.name }}</div>
            </div>
          </div>
        </div>
      </div>
      <div class="aside-holder">
        <div class="deal-box flex-col-v-center">
          <div class="title flex-center">成交看板</div>
          <div class="stat-box flex-col-v-center">
            <div class="name">总交易量</div>
            <div class="num">{{ stat.totalDeal }}</div>
          </div>
          <div class="item-group flex-row-v-center">
            <div class="num-box flex-1 flex-col-v-center">
              <div class="name">在售账号</div>
              <div class="num">{{ stat.totalAccount }}</div>
            </div>
            <div class="line"></div>
            <div class="num-box flex-1 flex-col-v-center">
              <div class="name">今日访问</div>
              <div class="num">{{ stat.todayVisit }}</div>
            </div>
          </div>
          <div class="btn flex-row-center" @click="goToNewPage(Core.Const.NET.END_POINT_WEB_TY + '/found-pay')">
            <img src="~assets/img/icon-cart.png"/>
            我要卖号
          </div>
        </div>
        <div class="check-box">
          <div class="check-item">
            <div class="title">客服验证</div>
            <div class="input-box flex-row-v-center">
              <input v-model="accountForService" placeholder="请输入账号" class="flex-1"/>
              <div class="btn flex-center" @click="verifyServiceAccount">点击验证</div>
            </div>
          </div>
          <div class="line"></div>
          <div class="check-item">
            <div class="title">黑号查询</div>
            <div class="input-box flex-row-v-center">
              <input v-model="accountForBlack" placeholder="请输入账号" class="flex-1"/>
              <div class="btn flex-center" @click="verifyBlackAccount">点击查询</div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <div class="sec-02">
      <div class="header-holder flex-row-space-between">
        <div>
          <div class="title">顶级账号</div>
          <div class="content">
          <span class="label" :class="{'active': item.id == accountRecommend.id}" v-for="item in gameList"
                :key="item.id"
                @click="onSelectAccountRecommend(item)">{{ item.name }}</span>
          </div>
        </div>
        <div class="more flex-row-v-center" @click="goToNewPage(Core.Const.NET.END_POINT_WEB_TY + '/account-on-sale')">查看全部
          <img src="~assets/img/icon-right-arrow.png"/>
        </div>
      </div>
      <div class="content-holder" v-if="accountRecommend.list.length > 0">
        <div class="flex-row-v-center" v-for="list in accountRecommend.list">
          <div class="card-box" v-for="item in list.val" @click="goToAccountDetailPage(item)">
            <div class="img-box" v-if="item.id">
              <img :src="item.img" :alt="item.title"/>
              <div class="hot-box flex-row-v-center">
                <img src="~assets/img/icon-hot.png">
                {{ item.hot }}
              </div>
            </div>
            <div class="content-box" v-if="item.id">
              <div class="name-txt">
                {{ item.title }}
              </div>
              <div class="line"></div>
              <div class="price-time-box flex-row-space-between">
                <div class="price-box flex-row-v-center">
                  <div class="unit">¥</div>
                  {{ item.price | moneyFormat }}
                </div>
                <div class="time-box flex-row-v-center">
                  <img src="~assets/img/icon-time.png">{{ item.launch_time }}
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="content-holder" v-else>
        <div class="empty-box flex-center">
          暂无顶级账号推荐~
        </div>
      </div>
    </div>

    <div class="sec-03">
      <div class="header-holder flex-row-space-between">
        <div>
          <div class="title">最近成交</div>
          <div class="content">
          <span class="label" :class="{'active': item.id == recentDeal.id}" v-for="item in gameList" :key="item.id"
                @click="onSelectRecentDeal(item)">{{ item.name }}</span>
          </div>
        </div>
        <div class="more flex-row-v-center">查看全部 <img src="~assets/img/icon-right-arrow.png"/></div>
      </div>
      <div class="content-holder">
        <div class="flex-row-v-center" v-if="topStat.count && dateStat.length > 0">
          <div class="card-box flex-col-v-center">
            <div class="img-box">
              <div class="num">{{ topStat.count }}/单</div>
              <img :src="topStat.img"/>
            </div>
            <div class="txt flex-center">顶级账号成交记录</div>
          </div>
          <div class="card-box flex-col-v-center" v-for="(item, idx) in dateStat" :key="idx">
            <div class="img-box">
              <div class="num">{{ item.count }}/单</div>
              <img :src="item.img" alt=""/>
            </div>
            <div class="txt flex-center">{{ item.date }}已出售</div>
          </div>
        </div>
        <div class="empty-box flex-center" v-else>
          暂无成交记录~
        </div>
      </div>
    </div>

    <Modal
      v-model="modal.show"
      title="提示"
      :mask-closable="false"
      width="360"
      @on-ok="onClickOk">
      <div v-if="modal.is_service && !modal.result">
        <p>注意：非梓良代售工作号！谨防受骗！</p>
      </div>

      <div v-if="modal.is_service && modal.result">
        <p>结果：是梓良代售工作号码！安心交易！</p>
        <p>创建时间：{{ modal.time }}</p>
        <p>备注：{{ modal.remark }}</p>
      </div>

      <div v-if="!modal.is_service && modal.result">
        <p>结果：该号码是黑名！谨防受骗！</p>
      </div>

      <div v-if="!modal.is_service && !modal.result">
        <p>结果：该号码不在黑名单中！</p>
      </div>
    </Modal>
  </div>
</template>

<script>
import Core from '@/core/core'

export default {
  data() {
    return {
      setting: {
        autoplay: true,
        autoplaySpeed: 5000,
        dots: 'inside',
        arrow: 'always'
      },
      carouselList: [],
      adList: [],
      gameList: [],
      selectGame: '',

      Core: Core,
      accountForService: '',
      accountForBlack: '',
      modal: {
        show: false,
        is_service: false,
        result: false,
        time: '',
        remark: ''
      },

      accountRecommend: {
        id: 0,
        list: []
      },
      recentDeal: {
        id: 0,
        list: []
      },
      topStat: {},
      dateStat: [],
      stat: {
        totalDeal: 0,
        totalAccount: 0,
        todayVisit: 0
      }
    }
  },
  mounted() {
    this.$jsonp('https://apis.map.qq.com/ws/location/v1/ip?key=5OLBZ-CM5CD-OL34L-HTXIK-YNOQE-LPFGB&output=jsonp')
      .then(res => {
        let ip = res.result.ip
        return Core.Api.Home.track({ip: ip})
      })
      .then(() => {
        console.log('埋点成功')
      })
      .catch(err => {
        console.log(err)
      })
    this.getHomeStat()
    this.getCarouselList()
    this.getRollAdList()
    setTimeout(() => {
      this.gameList = this.$store.state.gameList
      if (this.gameList.length > 0) {
        let id = this.gameList[0].id
        this.accountRecommend.id = id
        this.recentDeal.id = id
        this.getTopLevelList(id)
        this.getGameStat(id)
      }
    }, 100)

  },
  methods: {
    getHomeStat() {
      this._getHomeStat(res => {
        this.stat.totalDeal = res.totalDealOnSale
        this.stat.totalAccount = res.totalAccountOnSale
        this.stat.todayVisit = res.todayVisit
      })
    },
    getCarouselList() {
      this._getAdList(1, list => {
        this.carouselList = list
      })
    },
    getRollAdList() {
      this._getAdList(2, res => {
        let list = []
        for (let i = 0; i < res.length; i++) {
          list.push({
            id: res[i].id,
            content: res[i].title,
            link: res[i].url
          })
        }
        this.adList = list
      })
    },
    getTopLevelList(id) {
      this._getTopLevelList(id, 8, list => {
        let result = []
        if (list.length > 0 && list.length % 4 > 0) {
          let num = 4 - (list.length % 4);
          for (let i = 0; i < num; i++) {
            list.push({})
          }
        }
        for (let i = 0; i < list.length / 4; i++) {
          result.push({
            key: i,
            val: list.slice(4 * i, 4 * i + 4)
          })
        }
        this.accountRecommend.list = result
      })
    },
    getGameStat(id) {
      Core.Api.GameAccount.topLevelDeal({gameId: id})
        .then(res => {
          this.topStat = res
        })
        .catch(err => {
          this.$Message.error(err.msg);
        })
      Core.Api.GameAccount.statDeal({gameId: id})
        .then(res => {
          this.dateStat = res.list
        })
        .catch(err => {
          this.$Message.error(err.msg);
        })
    },
    verifyServiceAccount() {
      if (!this.accountForService.trim()) {
        this.$Message.warning("请输入账号进行客服验证");
        return
      }
      Core.Api.CustomerService.search({account: this.accountForService})
        .then(res => {
          this.modal.show = true
          this.modal.is_service = true
          if (res) {
            this.modal.result = true
            this.modal.time = res.create_time
            this.modal.remark = res.remark
          } else {
            this.modal.result = false
          }
        })
        .catch(err => {
          this.$Message.error(err.msg);
        })
    },
    verifyBlackAccount() {
      if (!this.accountForBlack.trim()) {
        this.$Message.warning("请输入账号进行黑号查询");
        return
      }
      Core.Api.BlackAccount.search({account: this.accountForBlack})
        .then(res => {
          this.modal.show = true
          this.modal.is_service = false
          if (res) {
            this.modal.result = true
          } else {
            this.modal.result = false
          }
        })
        .catch(err => {
          this.$Message.error(err.msg);
        })
    },
    onSelectAccountRecommend(item) {
      this.accountRecommend.id = item.id
      this.getTopLevelList(this.accountRecommend.id)
    },
    onSelectRecentDeal(item) {
      this.recentDeal.id = item.id
      this.getGameStat(this.recentDeal.id)
    },
    onClickOk() {

    },
    _getHomeStat(cb) {
      Core.Api.Home.stat({})
        .then(res => {
          if (cb) {
            cb(res)
          }
        })
        .catch(err => {
          this.$Message.error(err.msg);
        })
    },
    _getAdList(type, cb) {
      Core.Api.Ad.list({type: type})
        .then(res => {
          if (cb) {
            cb(res.list)
          }
        })
        .catch(err => {
          this.$Message.error(err.msg);
        })
    },
    _getTopLevelList(gameId, limit, cb) {
      Core.Api.GameAccount.topLevelList({gameId: gameId, limit: limit})
        .then(res => {
          if (cb) {
            cb(res.list)
          }
        })
        .catch(err => {
          this.$Message.error(err.msg);
        })
    },
    goToNewPage(url) {
      if (url) {
        window.open(url, '_blank')
      }
    },
    goToArticlePage(item) {
      if (item.article_id > 0) {
        this.$router.push(item.url + '?id=' + item.article_id)
      } else {
        this.$router.push(item.url)
      }
    },
    goToAccountDetailPage(item) {
      window.open(Core.Const.NET.END_POINT_WEB_TY + 'account-on-sale/detail?id=' + item.id, '_blank')
    }
  }
}
</script>

<style lang="scss" scoped>
.index-page {
  background: rgb(245, 245, 245);

  .sec-01 {
    padding-top: 20px;
    width: 1200px;
    margin: auto;

    .article-holder {
      margin-right: 20px;

      .carousel-box {
        width: 850px;
        height: 350px;
        background: #FFFFFF;

        .carousel-item {
          width: 850px;
          height: 350px;
          cursor: pointer;
        }
      }

      .ad-box {
        margin-top: 20px;
        width: 850px;
        height: 40px;
        background: #ffffff;
        padding: 0 9px;

        img {
          width: 22px;
          height: 22px;
          margin-right: 9px;
        }

        .content {
          font-size: 12px;
          color: #3A3D41;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
          cursor: pointer;
        }
      }

      .hot-game-box {
        margin-top: 20px;
        width: 850px;
        height: 218px;
        background: #ffffff;
        padding: 20px;

        .title {
          font-weight: 600;
          font-size: 20px;
          color: #3A3D41;
          line-height: 28px;
        }

        .content {
          padding: 30px 20px 0 20px;

          .item {
            margin-right: 58px;
            width: 80px;
            cursor: pointer;

            &:last-child {
              margin-right: 0;
            }

            &:hover {
              .name {
                color: #F58B01;
              }
            }

            img {
              width: 80px;
              height: 80px;
              border-radius: 12px;
            }

            .name {
              font-size: 14px;
              color: #3A3D41;
              text-align: center;
              line-height: 20px;
              margin-top: 12px;
            }
          }
        }
      }
    }

    .aside-holder {
      width: 330px;

      .deal-box {
        height: 350px;
        background: #FFFFFF;
        border-radius: 6px;
        padding: 0 20px 30px 20px;

        .title {
          width: 154px;
          height: 40px;
          background: #F5F5F5;
          font-weight: 600;
          font-size: 18px;
          color: #F58B01;
          background: url("~assets/img/banner-bg.png") center center;
          background-size: cover;
        }

        .stat-box {
          margin-top: 28px;

          .name {
            font-size: 14px;
            color: #3A3D41;
            line-height: 20px;
          }

          .num {
            font-weight: 500;
            font-size: 30px;
            color: #F58B01;
            margin-top: 2px;
            line-height: 42px;
          }
        }

        .item-group {
          width: 100%;
          margin-top: 40px;

          .num-box {
            .name {
              font-size: 14px;
              color: #3A3D41;
              line-height: 20px;
            }

            .num {
              font-weight: 500;
              font-size: 24px;
              color: #3A3D41;
              line-height: 33px;
            }
          }

          .line {
            width: 1px;
            height: 30px;
            background: rgb(245, 245, 245);
          }
        }

        .btn {
          background-image: linear-gradient(270deg, #FBBF02 0%, #F58A01 100%);
          border-radius: 4px;
          font-weight: 500;
          font-size: 18px;
          color: #FFFFFF;
          text-align: center;
          width: 100%;
          height: 60px;
          margin-top: 33px;
          cursor: pointer;

          img {
            width: 20px;
            height: 20px;
            margin-right: 5px;
          }
        }
      }

      .check-box {
        margin-top: 20px;
        height: 278px;
        padding: 20px;
        background: #FFFFFF;
        border-radius: 6px;

        .check-item {
          .title {
            font-weight: 600;
            font-size: 16px;
            color: #3A3D41;
            line-height: 22px;
          }

          .input-box {
            border: 1px solid #E6E8EB;
            border-radius: 25px;
            height: 40px;
            margin-top: 22px;
            padding: 2px 2px 2px 20px;

            input {
              height: 100%;
              margin-right: 4px;
              font-size: 14px;
              border: none;
              outline: none;
            }

            .btn {
              height: 34px;
              padding: 0 17px;
              background: #F58B01;
              border-radius: 18px;
              font-weight: 500;
              font-size: 14px;
              color: #FFFFFF;
              cursor: pointer;
            }
          }
        }

        .line {
          width: 100%;
          height: 1px;
          background: #EDEDED;
          margin-top: 35px;
          margin-bottom: 20px;
        }
      }
    }
  }

  .sec-02 {
    padding-top: 20px;
    width: 1200px;
    margin: auto;

    .header-holder {
      width: 100%;
      height: 122px;
      background: #FFFFFF;
      border-radius: 6px;
      padding: 20px;

      .title {
        font-weight: 600;
        font-size: 20px;
        color: #3A3D41;
        line-height: 28px;
      }

      .content {
        padding-top: 20px;
        font-size: 0;

        .label {
          margin-right: 14px;
          display: inline-block;
          background: #F5F5F5;
          border-radius: 17px;
          font-size: 14px;
          color: #7F8389;
          padding: 7px 16px;
          line-height: 20px;
          cursor: pointer;

          &.active {
            background: #F58B01;
            color: #FFFFFF;
          }

          &:last-child {
            margin-right: 0;
          }
        }
      }
    }

    .content-holder {
      width: 100%;

      .empty-box {
        background: #FFFFFF;
        margin-top: 20px;
        padding: 20px;
      }

      .card-box {
        margin-top: 20px;
        margin-right: 20px;
        flex: 1;
        background: #FFFFFF;
        border-radius: 6px;
        font-size: 0;
        overflow: hidden;
        cursor: pointer;

        &:last-child {
          margin-right: 0;
        }

        &:hover {
          .img-box img {
            transform: scale(1.2);
          }
        }

        .img-box {
          position: relative;
          height: 200px;
          overflow: hidden;
          font-size: 0;

          img {
            width: 100%;
            height: 200px;
            transition: all 0.6s;
          }

          .hot-box {
            background: rgba(0, 0, 0, .6);
            border-radius: 20px 0 0 20px;
            font-size: 14px;
            color: #FFFFFF;
            position: absolute;
            top: 20px;
            right: 0;
            padding: 0 12px;
            height: 30px;

            img {
              width: 14px;
              height: 14px;
              margin-right: 2px;
            }
          }
        }

        .content-box {
          padding: 0 14px;

          .name-txt {
            margin: 14px 0;
            font-size: 14px;
            color: #3A3D41;
            line-height: 20px;
            height: 60px;
            overflow: hidden;
            text-overflow: ellipsis;
            display: -webkit-box;
            -webkit-line-clamp: 3;
            line-clamp: 3;
            -webkit-box-orient: vertical;
          }

          .line {
            width: 100%;
            height: 0;
            border-top: 1px dashed #ECECEC;
          }

          .price-time-box {
            height: 40px;

            .price-box {
              font-weight: 500;
              font-size: 20px;
              color: #F0562D;
              line-height: 28px;

              .unit {
                font-size: 13px;
                line-height: 18px;
                margin-right: 4px;
                align-self: flex-end;
                position: relative;
                bottom: 3px;
              }
            }

            .time-box {
              font-size: 12px;
              color: #7F8389;

              img {
                width: 14px;
                height: 14px;
                margin-right: 4px;
              }
            }
          }
        }
      }
    }
  }

  .sec-03 {
    padding: 20px 0 6px 0;
    width: 1200px;
    margin: auto;

    .header-holder {
      width: 100%;
      height: 122px;
      background: #FFFFFF;
      border-radius: 6px;
      padding: 20px;

      .title {
        font-weight: 600;
        font-size: 20px;
        color: #3A3D41;
        line-height: 28px;
      }

      .content {
        padding-top: 20px;
        font-size: 0;

        .label {
          margin-right: 14px;
          display: inline-block;
          background: #F5F5F5;
          border-radius: 17px;
          font-size: 14px;
          color: #7F8389;
          padding: 7px 16px;
          line-height: 20px;
          cursor: pointer;

          &.active {
            background: #F58B01;
            color: #FFFFFF;
          }

          &:last-child {
            margin-right: 0;
          }
        }
      }
    }

    .content-holder {
      width: 100%;

      .empty-box {
        background: #FFFFFF;
        margin-top: 20px;
        padding: 20px;
      }

      .card-box {
        margin-top: 20px;
        background: #FFFFFF;
        margin-right: 20px;
        flex: 1;
        border-radius: 6px;
        overflow: hidden;
        cursor: pointer;

        &:hover {
          .txt {
            color: #F58B01;
          }
        }

        &:last-child {
          margin-right: 0;
        }

        .img-box {
          height: 200px;
          font-size: 0;
          position: relative;

          img {
            width: 100%;
            height: 100%;
          }

          .num {
            position: absolute;
            right: 0;
            top: 0;
            padding: 0 8px;
            line-height: 22px;
            border-bottom-left-radius: 10px;
            z-index: 10;
            background: #F58B01;
            font-size: 14px;
            color: #ffffff;
          }
        }

        .txt {
          width: 100%;
          height: 54px;
          padding: 10px;
        }
      }
    }
  }

  .more {
    font-size: 14px;
    color: #A6ACB3;
    cursor: pointer;

    img {
      width: 14px;
      height: 14px;
      margin-left: 2px;
    }
  }
}
</style>
